$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'busi/news/list',
        datatype: "json",
        headers: {
            "token": token
        },
        colModel: [			
        	{ label: '新闻ID', name: 'newsId', index: "newsId", width: 45, key: true,hidden:true },
			{ label: '新闻标题', name: 'title',index: "title", width: 150},
            { label: '副标题', name: 'subhead', width: 50 },
			{ label: '新闻类型', name: 'newsType', width: 80, formatter: function(value, options, row) {
					var ret = '<span class="label label-danger">院内</span>';
					if(value === 1) {
						ret = '<span class="label label-danger">行业</span>';
					} else if(value === 2) {
						ret = '<span class="label label-danger">通知</span>';
					}
					return ret;
			}},
			{ label: '内容', name: 'content', index: "content", width: 200,hidden:true },
			{ label: '发布时间', name: 'publishTime', index: "publishTime", width: 100},
			{ label: '阅读量', name: 'pageviews', index: "pageviews", width: 100}
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});


var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			title: null
		},
		showList: true,
		title:null,
		news:{
			newsType:0,
			newsId:null,
            title:null,
            content:null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.news = {title:null, subhead:null, newType:0, contetn:null};
			um.setContent('');
		},
		update: function () {
			var newsId = getSelectedRow();
			if(newsId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
    		vm.getNews(newsId);
		},
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
			        headers: {
			            "token": token
			        },
				    url: baseURL + "busi/news/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getNews: function(newsId){
			$.get(baseURL + "busi/news/info/"+newsId, { "token": token},function(r){
				vm.news = r.news;
				if(r.news.content) {
					um.setContent(vm.news.content);
				} else {
					um.setContent("");
				}
				um.reset();
			});
		},
		
		saveOrUpdate: function () {
			var url = vm.news.newsId == null ? "busi/news/save" : "busi/news/update";
			$('#ueContent').val(um.getContent());
			$.ajax({
				type: "POST",
		        headers: {
		            "token": token
		        },
			    url: baseURL + url,
			    data: {"title":vm.news.title,"subhead":vm.news.subhead,"newsType":vm.news.newsType,"content":vm.news.content,"newsId":vm.news.newsId},
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function () {
			vm.showList = true;
			um.setContent('');
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'title': vm.q.title},
                page:page
            }).trigger("reloadGrid");
		}
	}
});
